﻿/// <reference path="jquery-1.2.6.js" />

var map = null;
var points = [];
var shapes = [];
var center = null;

function LoadMap(latitude, longitude, onMapLoaded) {
    map = new VEMap('theMap');
    options = new VEMapOptions();
    options.EnableBirdseye = true;

    // Makes the control bar less obtrusize.
    map.SetDashboardSize(VEDashboardSize.Small);

    if (onMapLoaded != null)
        map.onLoadMap = onMapLoaded;

    if (latitude != null && longitude != null) {
        center = new VELatLong(latitude, longitude);
    }

    map.LoadMap(center, null, null, null, null, null, null, options);
}

function LoadPin(LL, name, description, address, website) {
    var shape = new VEShape(VEShapeType.Pushpin, LL);

    //Make a nice Pushpin shape with a title and description
    shape.SetTitle("<span class=\"pinTitle\"> " + escape(name) + "</span>");
    if (website !== undefined || description!== undefined) {
        shape.SetDescription("<p class=\"pinDetails\"><a href=\"http://" +
        escape(website) + "\" target=\"_blank\">" +
        escape(website) + "</a></p>" +
        "<p class=\"pinDetails\">" + 
        escape(address) + "</p>" +
        "<p class=\"pinDetails\">" + escape(description) + "</p>");
    }
    map.AddShape(shape);
    points.push(LL);
    shapes.push(shape);
}

function FindAddressOnMap(where) {
    var numberOfResults = 20;
    var setBestMapView = true;
    var showResults = true;

    map.Find("", where, null, null, null,
           numberOfResults, showResults, true, true,
           setBestMapView, callbackForLocation);
}

function callbackForLocation(layer, resultsArray, places,
            hasMore, VEErrorMessage) {

    clearMap();

    if (places == null)
        return;

    //Make a pushpin for each place we find
    jQuery.each(places, function(i, item) {
        var description = "";
        if (item.Description !== undefined) {
            description = item.Description;
        }
        var LL = new VELatLong(item.LatLong.Latitude,
                        item.LatLong.Longitude);

        LoadPin(LL, item.Name, description, "", "");
    });

    //Make sure all pushpins are visible
    if (points.length > 1) {
        map.SetMapView(points);
    }

    //If we've found exactly one place, that's our address.
    if (points.length === 1) {
        jQuery("#Latitude").val(points[0].Latitude);
        jQuery("#Longitude").val(points[0].Longitude);
    }
}

function clearMap() {
    map.Clear();
    points = [];
    shapes = [];
}


function FindGivenLocation(where) {
    map.Find("", where, null, null, null, null, null, false,
       null, null, callbackUpdateMap);
}

function callbackUpdateMap(layer, resultsArray, places, hasMore, VEErrorMessage) {
    //$("#dinnerList").empty();
    clearMap();
    var center = map.GetCenter();

    jQuery.post("/Search/SearchCourses", null, function(courses) {
    jQuery.each(courses, function(i, course) {

            var LL = new VELatLong(course.Latitude, course.Longitude, 0, null);

            // Add Pin to Map
            LoadPin(LL, '<a href="/Courses/Courses/Details/' + course.CourseID + '">'
                        + course.Title + '</a>');

        });

        // Adjust zoom to display all the pins we just added.
        if (points.length > 1) {
            map.SetMapView(points);
        }

        // Display the event's pin-bubble on hover.
        jQuery(".courseboxsmall").each(function(i, dinner) {
        jQuery(dinner).hover(
                function() { map.ShowInfoBox(shapes[i]); },
                function() { map.HideInfoBox(shapes[i]); }
            );
        });
    }, "json");
}
